package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.VoteDTO;
import com.zhiwen.service.UserVoteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 点赞控制器
 */
@Tag(name = "回答点赞", description = "回答点赞相关接口")
@RestController
@RequestMapping("/votes")
@RequiredArgsConstructor
public class VoteController {

    private final UserVoteService userVoteService;

    /**
     * 点赞回答
     */
    @Operation(summary = "点赞回答", description = "对回答进行点赞，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @PostMapping
    @SaCheckLogin
    public Result<Boolean> voteAnswer(@RequestBody @Valid VoteDTO voteDTO) {
        boolean result = userVoteService.voteAnswer(voteDTO);
        return Result.success(result);
    }

    /**
     * 取消点赞
     */
    @Operation(summary = "取消点赞", description = "取消对回答的点赞，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @DeleteMapping("/answer/{answerId}")
    @SaCheckLogin
    public Result<Boolean> cancelVote(@Parameter(description = "回答ID") @PathVariable Long answerId) {
        boolean result = userVoteService.cancelVote(answerId);
        return Result.success(result);
    }

    /**
     * 检查点赞状态
     */
    @Operation(summary = "检查点赞状态", description = "检查当前用户对回答的点赞状态，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @GetMapping("/answer/{answerId}/status")
    @SaCheckLogin
    public Result<Integer> checkVoteStatus(@Parameter(description = "回答ID") @PathVariable Long answerId) {
        Integer status = userVoteService.checkVoteStatus(answerId);
        return Result.success(status);
    }
}
